﻿using UnityEditor;
using UnityEngine;
using UnityEngine.UI;

[CustomEditor(typeof(CText))]
public class CTextEditor : UnityEditor.UI.TextEditor
{
    CText text;
    CTextEffect effect;
    int fontSize, outlineCount;
    SerializedProperty shadows;
    SerializedProperty outlines;
    bool haveChange;

    protected override void OnEnable()
    {
        base.OnEnable();
        text = (CText)target;
        effect = text.GetComponent<CTextEffect>();
        effect.enabled = true;
        shadows = serializedObject.FindProperty("shadows");
        outlines = serializedObject.FindProperty("outlines");
    }

    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();

        serializedObject.Update();

        if (fontSize != text.fontSize)
        {
            fontSize = text.fontSize;
            effect.Refresh();
            haveChange = true;
        }

        if (outlineCount != text.outlines.Length)
        {
            outlineCount = text.outlines.Length;
            effect.Refresh();
            haveChange = true;
        }

        EditorGUILayout.Space();

        //渐变
        CText.GradientColor _gradient = text.gradientColor;
        text.gradientColor = (CText.GradientColor)EditorGUILayout.EnumPopup("Gradient Color", text.gradientColor);
        if (text.gradientColor != CText.GradientColor.None)
        {
            EditorGUI.indentLevel++;
            Color _begin = text.begin;
            Color _end = text.end;
            text.begin = EditorGUILayout.ColorField("Begin", text.begin);
            text.end = EditorGUILayout.ColorField("End", text.end);
            if (_begin != text.begin || _end != text.end)
            {
                effect.Refresh();
                text.SetAllDirty();
                haveChange = true;
            }
            EditorGUI.indentLevel--;
        }
        if (_gradient != text.gradientColor)
        {
            effect.Refresh();
            text.SetAllDirty();
            haveChange = true;
        }

        EditorGUILayout.Space();

        //描边
        EditorGUILayout.PropertyField(outlines);

        EditorGUILayout.Space();

        //阴影
        EditorGUILayout.PropertyField(shadows);


        if (EditorGUI.EndChangeCheck())
        {
            serializedObject.ApplyModifiedProperties();
        }

        if (haveChange)
        {
            EditorUtility.SetDirty(target);
            haveChange = false;
        }
    }
}